package com.neu.controller;

import com.neu.base.Result;
import com.neu.entity.RDF;
import com.neu.service.RDFService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/rdf")
@Tag(name = "开发因素评估")
public class RDFController {
    @Autowired
    private RDFService rdfService;

    @PostMapping("/add")
    @Operation(summary = "添加RDF信息")
    public Result add(@RequestBody RDF rdf){

        return Result.success(rdfService.save(rdf));
    }

    @PostMapping("/get")
    @Operation(summary = "获取RDF信息")
    public Result getRDF(@RequestParam String id){
        RDF data = rdfService.getById(id);
        if(data == null){
            data = new RDF();
            data.setProgress(0);
            data.setProjectId(id);
            data.setDelFlag("1");
            rdfService.save(data);
        }
        System.out.println(data);
        Map rdf_data = data.calc();
//        Map map = new HashMap<>();
//        map.put("rdf_data", data);
//        map.put("data", rdf_data);

        return Result.success(data);
    }

    @PostMapping("/update")
    @Operation(summary = "更新RDF")
    public Result updateRDF(@RequestBody RDF rdf){

        try{
            rdf.calcRDF();
            rdf.setProgress(1);
        }catch (Exception e){
            rdf.setProgress(0);
        }

        Boolean sign = rdfService.updateById(rdf);
        if(!sign){
            sign = rdfService.save(rdf);
        }

        return Result.success(sign);
    }

}
